{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Version: 12.12.2018"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Description"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook showcases how to **search for modeling parameters** published in the **Blue Brain Knowledge Graph** backed by [Blue Brain Nexus](https://bluebrain.github.io/nexus/).\n",
    "\n",
    "For the features of the underlying toolkit, please refer to the [README](https://github.com/BlueBrain/nat/blob/master/kg/README.md) in the directory."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting Started"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "mkdir repos\n",
    "cd repos\n",
    "\n",
    "git clone -b master --depth=1 https://github.com/BlueBrain/nat.git\n",
    "\n",
    "conda create -yn kg_env python=3.7 jupyter requests\n",
    "conda activate kg_env\n",
    "\n",
    "jupyter notebook ./nat/kg/\"Literature Annotation - Querying Nexus.ipynb\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/fonta/repos\n"
     ]
    }
   ],
   "source": [
    "cd ../.."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## User variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"TOKEN\", \"r\") as f:\n",
    "    TOKEN = f.readline().rstrip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "DEPLOYMENT = \"https://bbp-nexus.epfl.ch/staging/v0\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "UTILS_DIR = \"./nat/kg\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Helpers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sys.path.append(UTILS_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "%autoreload 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "%aimport nexus_utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nexus_utils import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = SearchConfiguration(TOKEN, DEPLOYMENT, \"literatureannotation\", \"modelingparameter\", \"v1.0.6\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Annotations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by NAT ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 1\n"
     ]
    }
   ],
   "source": [
    "ra1 = sc.search_by_path(\"nsg:providerId\", \"5bec10d2-a1b4-11e6-8ba4-64006a4c56ef\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by reference ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 5\n"
     ]
    }
   ],
   "source": [
    "ra2 = sc.search_by_path(\"oa:hasTarget / oa:hasSource\", \"https://www.ncbi.nlm.nih.gov/pubmed/12509484\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by target type (text, table, ...)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 44\n",
      "<warning> 34 still to be retrieved\n"
     ]
    }
   ],
   "source": [
    "ra3 = sc.search_by_path(\"oa:hasTarget / rdf:type\", \"nsg:FigureTarget\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**For demonstration:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 44\n"
     ]
    }
   ],
   "source": [
    "ra3b = sc.search_by_path(\"oa:hasTarget / rdf:type\", \"nsg:FigureTarget\", limit=50)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by contributor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Family name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 91\n",
      "<warning> 81 still to be retrieved\n"
     ]
    }
   ],
   "source": [
    "ra4 = sc.search_by_path(\"nsg:contribution / prov:agent / schema:familyName\", \"Iavarone\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Family name and given name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 91\n",
      "<warning> 81 still to be retrieved\n"
     ]
    }
   ],
   "source": [
    "ra5 = sc.search_by_paths([\n",
    "    (\"nsg:contribution / prov:agent / schema:familyName\", \"Iavarone\"),\n",
    "    (\"nsg:contribution / prov:agent / schema:givenName\", \"Elisabetta\"),\n",
    "])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parameters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by NAT ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 1\n"
     ]
    }
   ],
   "source": [
    "rp1 = sc.search_by_path(\"nsg:providerId\", \"1dafc5f0-2f10-11e6-a79a-d0e140998c20\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by annotation NAT ID\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 1\n"
     ]
    }
   ],
   "source": [
    "rp2 = sc.search_by_path(\"^oa:hasBody / nsg:providerId\", \"5bec10d2-a1b4-11e6-8ba4-64006a4c56ef\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by reference ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 5\n"
     ]
    }
   ],
   "source": [
    "rp3 = sc.search_by_path(\"^oa:hasBody / oa:hasTarget / oa:hasSource\",\n",
    "                        \"https://www.ncbi.nlm.nih.gov/pubmed/12509484\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by data type (point value, numerical trace, ...)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 51\n",
      "<warning> 41 still to be retrieved\n"
     ]
    }
   ],
   "source": [
    "rp4 = sc.search_by_path(\"rdf:type\", \"nsg:NumericalTraceParameter\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by quantity type (BBP-xxxx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 29\n",
      "<warning> 19 still to be retrieved\n"
     ]
    }
   ],
   "source": [
    "rp5 = sc.search_by_path(\"nsg:dependentVariable / nsg:quantityType\", \"BBP-010002\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieve by contributor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Family name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 146\n",
      "<warning> 136 still to be retrieved\n"
     ]
    }
   ],
   "source": [
    "rp6 = sc.search_by_path(\"^oa:hasBody / nsg:contribution / prov:agent / schema:familyName\", \"Iavarone\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Family name and given name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count> 146\n",
      "<warning> 136 still to be retrieved\n"
     ]
    }
   ],
   "source": [
    "rp7 = sc.search_by_paths([\n",
    "    (\"^oa:hasBody / nsg:contribution / prov:agent / schema:familyName\", \"Iavarone\"),\n",
    "    (\"^oa:hasBody / nsg:contribution / prov:agent / schema:givenName\", \"Elisabetta\"),\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
